//
//  NSDate+Extension.m
//  ZiChanBao
//
//  Created by liujinliang on 15/6/23.
//  Copyright (c) 2015年 WorldUnion. All rights reserved.
//

#import "NSDate+Extension.h"

@implementation NSDate (Extension)
static NSDateFormatter *outputFormatter = nil;
//时间格式化 date时间 style样式
//type == 1: 根据时区改变
//type == 2: 固定样式
- (NSString *)formatterDatestyle:(NSString *)style type:(NSInteger)type
{
    if(type == 1)
        [[NSDate getOutputFormatter] setDateFormat:[NSDateFormatter dateFormatFromTemplate:style options:0 locale:[NSLocale currentLocale]]];
    else if(type == 2)
        [[NSDate getOutputFormatter] setDateFormat:style];
    
    return [[NSDate getOutputFormatter] stringFromDate:self];
}

-(NSString*)getDateFormateWithDateShow:(BOOL)dateShow  withTimeShow:(BOOL)timeShow
{
    
    if (dateShow==YES) {
        [[NSDate getOutputFormatter] setDateStyle:NSDateFormatterMediumStyle];
    }
    if (timeShow==YES) {
        [[NSDate getOutputFormatter] setTimeStyle:NSDateFormatterShortStyle];
    }
    
    NSString* time = [[NSDate getOutputFormatter] stringFromDate:self];
    //[outputFormatter release];
    return time;
}

//字符串转日期
+(NSDate*)formatterDate:(NSString *)style withDate:(NSString*)dateStr
{
    //    if(type == 1)
    //        [[NSDate getOutputFormatter] setDateFormat:[NSDateFormatter dateFormatFromTemplate:style options:0 locale:[NSLocale currentLocale]]];
    //    else if(type == 2)
    [[NSDate getOutputFormatter] setDateFormat:style];
    
    return [[NSDate getOutputFormatter] dateFromString:dateStr];
}

+ (NSDate * )NSStringToNSDate: (NSString * )dateStr withFormat:(NSString*)format
{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: format];
    NSDate *date = [formatter dateFromString :dateStr];
    return date;
}

+(NSDateFormatter *)getOutputFormatter
{

    if (outputFormatter==nil) {
        outputFormatter = [NSDateFormatter new];
    }
    return outputFormatter;
}


+(NSString *)timeIntervalToDataString:(NSTimeInterval)timeInterval  formate:(NSString *)formate
{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeInterval];

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:formate];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
}
@end
